{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bbcf95e7",
   "metadata": {},
   "source": [
    "# 本周知识点"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0cefe6e3",
   "metadata": {},
   "source": [
    "> 1. while循环\n",
    "> 2. 购物车练习"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f45a20e1",
   "metadata": {},
   "source": [
    "## 1.while循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4610efe0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你是风儿我是沙 0\n",
      "你是风儿我是沙 1\n",
      "你是风儿我是沙 2\n",
      "你是风儿我是沙 3\n",
      "你是风儿我是沙 4\n",
      "你是风儿我是沙 5\n",
      "你是风儿我是沙 6\n",
      "你是风儿我是沙 7\n",
      "你是风儿我是沙 8\n",
      "你是风儿我是沙 9\n",
      "你是风儿我是沙 10\n",
      "你是风儿我是沙 11\n",
      "你是风儿我是沙 12\n",
      "你是风儿我是沙 13\n",
      "你是风儿我是沙 14\n",
      "你是风儿我是沙 15\n",
      "你是风儿我是沙 16\n",
      "你是风儿我是沙 17\n",
      "你是风儿我是沙 18\n",
      "你是风儿我是沙 19\n",
      "你是风儿我是沙 20\n",
      "你是风儿我是沙 21\n",
      "你是风儿我是沙 22\n",
      "你是风儿我是沙 23\n",
      "你是风儿我是沙 24\n",
      "你是风儿我是沙 25\n",
      "你是风儿我是沙 26\n",
      "你是风儿我是沙 27\n",
      "你是风儿我是沙 28\n",
      "你是风儿我是沙 29\n",
      "你是风儿我是沙 30\n",
      "你是风儿我是沙 31\n",
      "你是风儿我是沙 32\n",
      "你是风儿我是沙 33\n",
      "你是风儿我是沙 34\n",
      "你是风儿我是沙 35\n",
      "你是风儿我是沙 36\n",
      "你是风儿我是沙 37\n",
      "你是风儿我是沙 38\n",
      "你是风儿我是沙 39\n",
      "你是风儿我是沙 40\n",
      "你是风儿我是沙 41\n",
      "你是风儿我是沙 42\n",
      "你是风儿我是沙 43\n",
      "你是风儿我是沙 44\n",
      "你是风儿我是沙 45\n",
      "你是风儿我是沙 46\n",
      "你是风儿我是沙 47\n",
      "你是风儿我是沙 48\n",
      "你是风儿我是沙 49\n"
     ]
    }
   ],
   "source": [
    "# while True 可以实现 for ＋range 的方法\n",
    "# while True 一般用于不确定的循环次数或者无限次循环操作\n",
    "count = 0\n",
    "while True:\n",
    "    print(\"你是风儿我是沙\",count)\n",
    "    count += 1\n",
    "    if count == 50: # 加入结束条件\n",
    "        break   # 跳出循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "24e82794",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hide_card:6\n"
     ]
    }
   ],
   "source": [
    "# 猜数字小游戏： 0<n<100,5次机会猜数字，每次猜完出题人提示你猜的有点大/有点小\n",
    "hide_card = int(input(\"hide_card:\"))\n",
    "count = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aabdeeda",
   "metadata": {},
   "outputs": [],
   "source": [
    "import getpass\n",
    "hide_card = int(getpass.getpass(\"hide_card:\"))\n",
    "count = 0\n",
    "if hide_card <100 and hide_card >0:\n",
    "        print(\"游戏开始\")\n",
    "        while True:\n",
    "            if count <5:\n",
    "                guess_digit = int(input(\"guess_digit\")) #玩游戏的人\n",
    "                if guess_digit == hide_card:\n",
    "                    print(\"yes,you got it.\")\n",
    "                    break\n",
    "                elif guess_digit > hide_card:\n",
    "                    print(\"往小的想想\")\n",
    "                else:\n",
    "                    print(\"往大的想想\")\n",
    "                count = count+1\n",
    "            else:\n",
    "                print(\"猜这么多次都不对,你个笨蛋!\")\n",
    "                print(\"正确答案：%s\"%(hide_card))\n",
    "                break\n",
    "else:\n",
    "    print(\"请输入数字在0-99之间。不要耍赖！\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bfb198cc",
   "metadata": {},
   "source": [
    "# 购物车"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8ce9a173",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：22\n",
      "----------商品列表----------\n",
      "商品序号 (0)： 蜜雪柠檬水 6元\n",
      "商品序号 (1)： 蜜桃四季春 10元\n",
      "商品序号 (2)： 芒果奶盖 12元\n",
      "商品序号 (3)： 葡萄波波奶茶 8元\n",
      "请输入商品编号：1\n",
      "商品购买成功，正在制作中...您的余额还有12元\n",
      "----------商品列表----------\n",
      "商品序号 (0)： 蜜雪柠檬水 6元\n",
      "商品序号 (1)： 蜜桃四季春 10元\n",
      "商品序号 (2)： 芒果奶盖 12元\n",
      "商品序号 (3)： 葡萄波波奶茶 8元\n",
      "请输入商品编号：2\n",
      "商品购买成功，正在制作中...您的余额还有0元\n",
      "----------商品列表----------\n",
      "商品序号 (0)： 蜜雪柠檬水 6元\n",
      "商品序号 (1)： 蜜桃四季春 10元\n",
      "商品序号 (2)： 芒果奶盖 12元\n",
      "商品序号 (3)： 葡萄波波奶茶 8元\n",
      "请输入商品编号：q\n",
      "您已购买的商品： [['蜜桃四季春', 10], ['芒果奶盖', 12]]\n",
      "您的余额还有0,欢迎下次光临~~\n"
     ]
    }
   ],
   "source": [
    "# 1. 运行程序后，让用户输入支付宝余额 ✔，然后打印我们的商品列表给用户。✔\n",
    "product_list = [\n",
    "    [\"蜜雪柠檬水\",6],\n",
    "    [\"蜜桃四季春\",10],\n",
    "    [\"芒果奶盖\",12],\n",
    "    [\"葡萄波波奶茶\",8]\n",
    "]\n",
    "shopping_car =[]\n",
    "AliPay = int(input(\"请输入余额：\"))\n",
    "while True:\n",
    "    print(\"----------商品列表----------\")\n",
    "    for i in range(4):\n",
    "        print(\"商品序号\",\"(\"+str(i)+\")：\",product_list[i][0],str(product_list[i][1])+\"元\")  # 格式化输出\n",
    "    # 2. 让用户输入商品编号进行商品的购买。:直接用商品列表的 index 作为商品标号\n",
    "    user_choice = input(\"请输入商品编号：\")\n",
    "    # 情景1：用户正在购买，输入商品编号\n",
    "    if user_choice.isdigit():\n",
    "        # 情景一：余额大于商品价格\n",
    "        if AliPay >= product_list[int(user_choice)][1]:\n",
    "            AliPay = AliPay - product_list[int(user_choice)][1]\n",
    "            shopping_car.append(product_list[int(user_choice)])   \n",
    "            print(\"商品购买成功，正在制作中...您的余额还有%s元\"%(AliPay))\n",
    "        # 情景二：余额小于商品价格\n",
    "        else:\n",
    "            print(\"您的余额不足，请更换支付方式~~~\")\n",
    "    # 情景2：用户输入退出，结束购买\n",
    "    elif user_choice == \"q\":\n",
    "        print(\"您已购买的商品：\",shopping_car)  \n",
    "        print(\"您的余额还有%s,欢迎下次光临~~\"%(AliPay)) \n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dad10c92",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
